﻿using System;
using System.Linq;
using tumblr.Api.Model;

namespace tumblr.Models.PostModel
{
    public class PostModelFactory
    {
        public static PostModelBase Create(Post post)
        {
            switch (post.type)
            {
                case "text":
                    return CreateTextPost(post);

                case "photo":
                    return CreatePhotoPost(post);

                case "quote":
                    return CreateQuotePost(post);

                case "link":
                    return CreateLinkPost(post);

                case "chat":
                    return CreateChatPost(post);

                case "audio":
                    return CreateAudioPost(post);

                case "video":
                    return CreateVideoPost(post);

                default:
                    throw new ArgumentException("Unknown post type", "post");
            }
        }

        private static VideoPostModel CreateVideoPost(Post post)
        {
            return new VideoPostModel(post.post_url)
            {
                Caption = post.caption
            };
        }

        private static AudioPostModel CreateAudioPost(Post post)
        {
            return new AudioPostModel(post.post_url)
            {
                Caption = post.caption
            };
        }

        private static ChatPostModel CreateChatPost(Post post)
        {
            return new ChatPostModel(post.post_url)
            {
                Conversation = post.body,
                Title = post.title
            };
        }

        private static LinkPostModel CreateLinkPost(Post post)
        {
            return new LinkPostModel(post.post_url)
            {
                Url = post.url,
                Title = post.title
            };
        }

        private static QuotePostModel CreateQuotePost(Post post)
        {
            return new QuotePostModel(post.post_url)
            {
                Quote = post.text,
                Source = post.source
            };
        }

        private static PhotoPostModel CreatePhotoPost(Post post)
        {
            return new PhotoPostModel(post.post_url)
            {
                Caption = post.caption,
                Urls = (from p in post.photos
                        select GetBestSizeUrl(p)
                        ).ToList()
            };
        }

        private static TextPostModel CreateTextPost(Post post)
        {
            return new TextPostModel(post.post_url)
            {
                Body = post.text
            };
        }

        private static string GetBestSizeUrl(Photo photo)
        {
            if (photo.alt_sizes.Any())
            {
                var alt = photo.alt_sizes
                    .Where(s => s.width > 100 && s.width < 480)
                    .OrderBy(s => s.height).FirstOrDefault();

                if (alt != null) return alt.url;
            }

            if(photo.original_size != null)
                return photo.original_size.url;

            return string.Empty;
        }
    }
}
